package server;

import java.io.*;

/**
 * 封装response需要依赖于OutputStream
 * 该对象需要提供核心方法,输出html
 * @author 陶肖寅
 * @version V1.0
 * @Package server
 * @date 2020-02-24 11:40
 * @Copyright © 2020-2021 城云科技(中国)有限公司
 */
public class Response {
    private OutputStream outputStream;

    public Response() {
    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public OutputStream getOutputStream() {
        return outputStream;
    }


    /**
     * 输出指定字符串
     */
    public void output(String content) throws IOException {
        outputStream.write(content.getBytes());

    }
    /**
     *
     * @param  path url,根据url获取静态资源的绝对路径，
     */
    public void outputHtml(String path) throws IOException {
        //获取静态资源文件
        String absoluteResourcePath=StaticResourceUtil.getAbsolutePath(path);
        File file = new File(absoluteResourcePath);
        if(file.exists()){
            //如果资源存在
            StaticResourceUtil.outputStaticReource(new FileInputStream(file),outputStream);
        } else {
            //如果不存在则404;
            output(HttpProtocolUtil.getHttpHeader404());
        }
    }
}
